package ai.platon.pulsar.heavy

import ai.platon.pulsar.common.AppPaths
import java.nio.file.Path
import java.time.OffsetDateTime
import java.util.concurrent.ConcurrentSkipListSet

object TestResourceHelper {

    /**
     * Generate [n] temporary files in the local file system before all the tests.
     * */
    fun generateTestFiles(n: Int = 10000): ConcurrentSkipListSet<Path> {
        val baseDir = AppPaths.getProcTmpTmp("PerformanceTest")

        val testPaths = ConcurrentSkipListSet<Path>()

        baseDir.toFile().mkdirs()
        IntRange(0, n).mapTo(testPaths) { i -> baseDir.resolve("test-$i.html") }

        testPaths.forEachIndexed { index, path ->
            try {
                path.toFile().writeText(
                    """
<html><body>
<h2>The $index test page</h2>

<p>Hello World!</p>
<hr/>
<div>${OffsetDateTime.now()}</div>
<div>Generated by PulsarRPA!</p></div>
</body></html>
                        """.trimIndent()
                )
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        println("Generated test files, size: ${testPaths.size}")

        return testPaths
    }

}